import java.util.LinkedList;
import java.util.List;

public class LeastAverage {
	public static void main(String args[]) {
		List<Integer> list = new LinkedList<Integer>();
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
		// Test all positive
		System.out.println(maxDivision(list));
		list.clear();
		list.add(-1);
		list.add(-2);
		list.add(-3);
		list.add(-4);
		// Test all negative
		System.out.println(maxDivision(list));
		list.clear();
		list.add(0);
		list.add(-1);
		list.add(1);
		// Test a solution where the best answer is '0'
		System.out.println(maxDivision(list));
		list.add(2);
		// Test a solution with '0' where the best answer is positive
		System.out.println(maxDivision(list));
		list.add(-5);
		// Test a solution where the two negative values form a better answer
		// than the two positive
		System.out.println(maxDivision(list));
		list.clear();
		list.add(-1);
		list.add(0);
		// Test a two value solution where 0 is best (negative)
		System.out.println(maxDivision(list));
		list.clear();
		list.add(1);
		list.add(0);
		// Test a two value solution where 0 is best (positive)
		System.out.println(maxDivision(list));
		list.clear();
		list.add(1);
		list.add(-1);
		// Test a two value solution with a negative answer
		System.out.println(maxDivision(list));
		list.clear();
		list.add(5);
		list.add(-1);
		list.add(-2);
		list.add(-3);
		list.add(-5);
		// Test a > 2 value solution with a negative answer
		System.out.println(maxDivision(list));
		list.clear();
		// Test error
		System.out.println(maxDivision(list));
	}

	public static double maxDivision(List<Integer> list)
			throws IllegalArgumentException {
		if (list.size() < 2) {
			throw new IllegalArgumentException(
					"List must contain at least two entries");
		}
		double retval = 0.0;
		boolean foundZero = false;
		boolean foundMaxPositive = false;
		boolean foundMinPositive = false;
		boolean foundMaxNegative = false;
		boolean foundMinNegative = false;
		int maxPositive = Integer.MIN_VALUE;
		int minPositive = Integer.MAX_VALUE;
		int maxNegative = Integer.MAX_VALUE;
		int minNegative = Integer.MIN_VALUE;
		// Iterate over the list
		for (Integer integer : list) {
			// If the integer is positive
			if (integer > 0) {
				if (integer > maxPositive) {
					// Set the flag that we found a positive value
					if (!foundMaxPositive) {
						foundMaxPositive = true;
						// If there's a new maximum but we haven't found a min,
						// shift
						// old maximum into the first minimum
					} else if (!foundMinPositive) {
						foundMinPositive = true;
						minPositive = maxPositive;
					}
					maxPositive = integer;
				} else if (integer < minPositive) {
					minPositive = integer;
					// Set the flag that we found a minimum
					if (!foundMinPositive) {
						foundMinPositive = true;
					}
				}
				// if the integer is negative
			} else if (integer < 0) {
				if (integer < maxNegative) {
					// set the flag that we found a maximum value
					if (!foundMaxNegative) {
						foundMaxNegative = true;
					// If there's a new maximum but we haven't found a min, shift
					// old maximum into the first minimum
					} else if (!foundMinNegative) {
						foundMinNegative = true;
						minNegative = maxNegative;
					}
					maxNegative = integer;
				} else if (integer > minNegative) {
					minNegative = integer;
					// set the flage that we found a minimum value
					if (!foundMinNegative) {
						foundMinNegative = true;
					}
				}
				// if the integer is zero
			} else if (!foundZero) {
				foundZero = true;
			}
		}
		// There will be a positive solution, so divide the max by the min
		// to get the best
		if (foundMaxPositive && foundMinPositive) {
			retval = (double) maxPositive / (double) minPositive;
		}
		// Negative divided by a negative will give us a positive, so divide
		// the max by the min and compare it to the value found in the step above
		if (foundMinNegative && foundMaxNegative) {
			double tempMin = (double) maxNegative / (double) minNegative;
			// If we found a value in the step above, compare to find the largest
			// If we didn't, than 'retval' will be 0, and tempMin will be larger
			if (tempMin > retval) {
				retval = tempMin;
			}
		}
		// Haven't found a viable positive solution yet; check negatives
		if (retval == 0.0) {
			// '0' is greater than all negatives
			if (foundZero) {
				retval = 0.0;
			// The list has two elements: one positive and one negative
			} else {
				double maxValue = Math.max(maxPositive, maxNegative * -1);
				double minValue = Math.min(maxPositive, maxNegative * -1);
				retval = -1 * ((double) maxValue / (double) minValue);
			}
		}
		return retval;
	}
}
